﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Media;

namespace AirWar
{
    //枚举游戏状态
    public enum GameState
    { 
        Open = 1, Close = 2, Pause = 3
    }

    //枚举移动方向
    public enum Direction
    { 
        Up = 1, Down = 2, Left = 3, Right = 4
    }

    //枚举敌方和我方
    public enum Side
    {
        Me = 1, Enemy = 2
    }

    //飞机类
    public class Fighter
    {
        #region 私有属性
        //飞机所属方
        private Side _side;
        //飞机坐标
        private Point _postion;
        //飞机移动方向
        private Direction _direction;
        //飞机步长
        private int _step;
        //飞机尺寸大小
        private Size _size;
        //飞机生命值
        private int _lifeValue;
        //飞机的得分
        private int _score;
        
        //飞机当前显示的图片
        private Bitmap _curFightBmp;
        //飞机候选图片的数组
        private Bitmap[] _fighterBmp = new Bitmap[4];
        //飞机的图片轮换标志
        private bool _bmpChangeFlag = true;
        //飞机的爆炸图片数组
        private Bitmap[] _explodeBitmap = new Bitmap[6];
        #endregion
        # region 公有属性
        public Side _Side
        {
            get { return _side; }
            set { _side = value; }
        }
        public Point _Postion
        {
            get { return _postion; }
            set { _postion = value; }
        }
        public Direction _Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        public int _Step
        {
            get { return _step; }
            set { _step = value; }
        }
        public Size _Size
        {
            get { return _size; }
            set { _size = value; }
        }
        public int _LifeValue
        {
            get { return _lifeValue; }
            set { _lifeValue = value; }
        }
        public int _Score
        {
            get { return _score; }
            set { _score = value; }
        }
        # endregion
        public Fighter(Side side, Size mapSize)
        { 
            //设置飞机所属方
            _side = side;
            //如果是我方飞机
            if (side == Side.Me)
            { 
                //装载我方飞机的候选图片
                _fighterBmp[0] = new Bitmap("Image\\myFighterUpDown.png");
                _fighterBmp[1] = new Bitmap("Image\\myFighterUpDownLongFlame.png");
                _fighterBmp[2] = new Bitmap("Image\\myFighterLeft.png");
                _fighterBmp[3] = new Bitmap("Image\\myFighterRight.png");
                //设置我们候选图片的透明色
                for (int i = 0; i <= 3; i++)
                {
                    _fighterBmp[i].MakeTransparent(Color.Black);
                }
                //设置我方飞机当前显示的图片
                _curFightBmp = _fighterBmp[0];

                //设置我们飞机的尺寸大小
                _size = new Size(_curFightBmp.Width, _curFightBmp.Height);
                //设置我方飞机的初始移动方向
                _direction = Direction.Up;
                //设置我方飞机的初始位置在地图下方正中
                _postion.X = mapSize.Width / 2 - _size.Width / 2;
                _postion.Y = mapSize.Height - _size.Height - 25;

                //设置我方飞机步长为10，默认生命值为100，默认得分为0
                _step = 10;
                _lifeValue = 100;
                _score = 0;
            }
            //如果是敌方飞机
            else if (side == Side.Enemy)
            {
                //加载候选图
                _fighterBmp[0] = new Bitmap("Image\\enemyFighterGreen.png");
                _fighterBmp[1] = new Bitmap("Image\\enemyFighterRed.png");
                _fighterBmp[2] = new Bitmap("Image\\enemyFighterYellow.png");
                //设置透明色
                for (int i = 0; i <= 2; i++)
                    _fighterBmp[i].MakeTransparent(Color.Black);
                //随机选一张作为敌方飞机的图片
                Random rand = new Random(DateTime.Now.Second);
                _curFightBmp = _fighterBmp[rand.Next(0, 3)];

                //设置尺寸
                _size = new Size(_curFightBmp.Width, _curFightBmp.Height);

                //设置初始移动方向向下
                _direction = Direction.Down;

                //设置初始位置
                _postion.X = rand.Next(0, mapSize.Width);
                _postion.Y = _size.Height - _size.Height;

                //设置飞机默认步长为6，默认生命值为2，默认得分为0
                _step = 6;
                _lifeValue = 2;
                _score = 0;

                //装载六张飞机爆炸图片
                for (int i = 0; i <= 5; i++)
                {
                    _explodeBitmap[i] = new Bitmap("Image\\explode" + i.ToString() + ".png");
                    _explodeBitmap[i].MakeTransparent(Color.Black);
                }
            }
        }
        //飞机绘制
        public void DrawMe(Graphics g)
        { 
            //绘制当前图片
            g.DrawImage(_curFightBmp, _postion);
        }

        //飞机移动
        public void Move(Direction direction, Size mapSize)
        { 
            //保存移动方向
            _direction = direction;
            //如果是我方飞机
            if (_side == Side.Me)
            {
                //如果向上移动
                if (_direction == Direction.Up)
                {
                    //设置移动后的位置
                    if (_postion.Y >= _step)//确保不会飞出边界
                        _postion.Y = _postion.Y - _step;
                    else
                        _postion.Y = 0;//否则对齐最上端
                    //设置飞机当前的图片
                    //为了更逼真，需要在【0】【1】之间轮换
                    if (_bmpChangeFlag == true)
                        _curFightBmp = _fighterBmp[0];
                    else
                        _curFightBmp = _fighterBmp[1];
                    //切换轮换标志
                    _bmpChangeFlag = !_bmpChangeFlag;
                }
                //如果向下移动
                else if (_direction == Direction.Down)
                {
                    //设置飞机移动后的所在的位置
                    if (_postion.Y <= mapSize.Height - _size.Height - _step)//确保不会飞出下边界
                        _postion.Y = _postion.Y + _step;
                    else
                        _postion.Y = mapSize.Height - _size.Height;
                    //设置当前显示图片
                    _curFightBmp = _fighterBmp[0];
                }
                //如果向左移动
                else if (_direction == Direction.Left)
                {
                    //设置位置
                    if (_postion.X >= _step)
                        _postion.X = _postion.X - _step;
                    else
                        _postion.X = 0;
                    _curFightBmp = _fighterBmp[2];
                }
                else if (_direction == Direction.Right)
                {
                    if (_postion.X <= mapSize.Width - _size.Width - _step)
                        _postion.X = _postion.X + _step;
                    else
                        _postion.X = mapSize.Width - _size.Width;
                    _curFightBmp = _fighterBmp[3];
                }
                //重新设置飞机的尺寸大小（不同方向存在尺寸差异）
                _size = new Size(_curFightBmp.Width, _curFightBmp.Height);
            }
            //敌方飞机
            else
            { 
                //如果向下移动
                if (_direction == Direction.Down)
                    _postion.Y = _postion.Y + _step;
                //向左下移动
                else if (_direction == Direction.Left)
                {
                    if (_postion.X > _step)//不能飞出左侧
                    {
                        _postion.X = _postion.X - _step;
                        _postion.Y = _postion.Y + _step;
                    }
                    else
                    {
                        _postion.X = 0;
                        _postion.Y = _postion.Y + _step;
                    }
                }
                //向右下移动
                else if (_direction == Direction.Right)
                {
                    if (_postion.X <= mapSize.Width - _size.Width - _step)
                    {
                        _postion.X = _postion.X + _step;
                        _postion.Y = _postion.Y + _step;
                    }
                    else
                    {
                        _postion.X = mapSize.Width - _size.Width;
                        _postion.Y = _postion.Y + _step;
                    }
                }
            }
        }
        //飞机开火,给子弹设置位置
        public Bullet Fire()
        { 
            //创建一个子弹类对象
            Bullet bullet = new Bullet(_side);
            //如果是我方子弹
            if (_side == Side.Me)
            {
                //根据发射方向不同，设置初始位置
                if (_direction == Direction.Up || _direction == Direction.Down)
                    bullet._Position = new Point(_Postion.X + _size.Width / 2 - 4, _postion.Y - _size.Height / 2);
                else if (_direction == Direction.Left)
                    bullet._Position = new Point(_Postion.X + _size.Width / 2 - 6, _postion.Y - _size.Height / 2);
                else if (_direction == Direction.Right)
                    bullet._Position = new Point(_Postion.X + _size.Width / 2 - 2, _postion.Y - _size.Height / 2);
            }
            //如果是敌方子弹
            else
                //设置敌方子弹的初始位置
                bullet._Position = new Point(_Postion.X + _size.Width / 2 - 4, _postion.Y - _size.Height / 2);
            //返回子弹对象
            return bullet;
        }
        //飞机爆炸
        public void Explode(Graphics g)
        { 
            //逐一绘制6幅爆炸图片
            for (int i = 0; i <= 5; i++)
                g.DrawImage(_explodeBitmap[i], _Postion.X, _Postion.Y);
        }
        //播放音效
        public void PlaySound(String soundFile)
        { 
            //播放音效
            SoundPlayer soundPlay = new SoundPlayer(soundFile);
            soundPlay.Play();
        }
    }
}
